#ifndef JTHREAD_H
#define JTHREAD_H

#include <JXQCore/JMessage>

namespace JXQ
{

class JThreadPrivate;

#ifdef J_OS_WIN
typedef JHandle   JTid;
#else
typedef pthread_t JTid;
#endif

class JXQLIB_EXPORT JThread
{
	J_MESSAGE(finished)

public:
	JThread();
	virtual ~JThread() = 0;

public:
	void start() noexcept;
	bool wait(int msecs = -1);
	void detach();

public:
	bool isFinished() const;
	void setEnableDeleteOnFinished(bool enable = true);
	bool isEnableDeleteOnFinished() const;

protected:
	virtual void run() = 0;

public:
	static JTid selfID();
	static void usleep(uint32_t usecs);
	static void msleep(uint32_t msecs);
	static void sleep(uint32_t secs);
	static void sleep(uint32_t secs, uint32_t msecs);
	static void sleep(uint32_t secs, uint32_t msecs, uint32_t usecs);

public:
	bool termination(); //不建议使用
	static int getCPUCoreCount();

private:
	friend class JThreadPrivate;
	J_DISABLE_COPY(JThread)
	JThreadPrivate *d_ptr;
};

} //namespace JXQ


#endif // JTHREAD_H
